/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simulator;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

/**
 *
 * @author vdelaitr
 */
public class RendererJ3D implements Renderer {

    /* J3D variables */
    private MainFrame frame;
    private SimpleUniverse universe;

    private static BranchGroup content;
   
    public RendererJ3D() {
        // Create display frame
        Applet applet = new Applet();
        applet.setLayout(new BorderLayout());

        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas = new Canvas3D(config);
        applet.add(canvas);

        universe = new SimpleUniverse(canvas);
        universe.getViewingPlatform().setNominalViewingTransform();

        frame = new MainFrame(applet, 640, 480);
        frame.setTitle("The Awesome Willocopter !!!");
    }

    public void addDirectionalLight(Vector3f direction, Color3f color) {
        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        DirectionalLight light = new DirectionalLight(color, direction);
        light.setInfluencingBounds(bounds);
        content.addChild(light);
    }
    
   /* void create_cube() {
        //cube
        Appearance ap = getAppearance(new Color3f(Color.blue));
        ap.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
        ap.setCapability(Appearance.ALLOW_TEXGEN_WRITE);
        Vector3d vector = new Vector3d(-0.3f, 0.0f, 0.0f);
        Transform3D transform = new Transform3D();
        transform.setTranslation(vector);
        Box box = new Box(.5f, .5f, .5f, Primitive.GENERATE_TEXTURE_COORDS,
				getAppearance(new Color3f(Color.green)));
        box.setCapability(Box.ENABLE_APPEARANCE_MODIFY);
        box.setCapability(Box.GEOMETRY_NOT_SHARED);
        Shape3D frontShape = box.getShape(Box.FRONT);
        frontShape.setAppearance(ap);
        box.getShape(Box.TOP).setAppearance(getAppearance(Color.magenta));
        box.getShape(Box.BOTTOM).setAppearance(getAppearance(Color.orange));
        box.getShape(Box.RIGHT).setAppearance(getAppearance(Color.red));
        box.getShape(Box.LEFT).setAppearance(getAppearance(Color.green));
        box.getShape(Box.BACK).setAppearance(getAppearance(new Color3f(Color.yellow)));

        TransformGroup boxTransformGroup = new TransformGroup();
        boxTransformGroup.addChild(box);

        BranchGroup group = new BranchGroup();
        group.addChild(boxTransformGroup);

        universe.addBranchGraph(group);
    }*/

/*
    public void addTexture(BufferedImage image, Shape3D shape) {
            shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
            appearance = shape.getAppearance();
            appearance.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_WRITE);
            appearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
            appearance.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
            changeTexture( texture,  image,  shape);
            Color3f col = new Color3f(0.0f, 0.0f, 1.0f);
            ColoringAttributes ca = new ColoringAttributes(col,
                            ColoringAttributes.NICEST);
            appearance.setColoringAttributes(ca);

            }
    public void changeTexture(Texture texture, BufferedImage image, Shape3D shape) {
    	loader = new TextureLoader(image, "RGB",
				TextureLoader.ALLOW_NON_POWER_OF_TWO);
    	texture = loader.getTexture();
		texture.setBoundaryModeS(Texture.CLAMP_TO_BOUNDARY);
		texture.setBoundaryModeT(Texture.CLAMP_TO_BOUNDARY);
		texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.5f, 0f));
		// Set up the texture attributes
		// could be REPLACE, BLEND or DECAL instead of MODULATE

		// front = getAppearance(new Color3f(Color.YELLOW));
		Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
		Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
		Color3f red = new Color3f(0.7f, .15f, .15f);
		appearance.setMaterial(new Material(red, black, red, white, 1.0f));
		TextureAttributes texAttr = new TextureAttributes();
		texAttr.setTextureMode(TextureAttributes.REPLACE);
		appearance.setTextureAttributes(texAttr);
		appearance.setTexture(texture);
		shape.setAppearance(appearance);
	}
*/
/*
    public static Appearance getAppearance(Color color) {
            return getAppearance(new Color3f(color));
    }
    public static Appearance getAppearance(Color3f color) {
            Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
            Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
            Appearance ap = new Appearance();
            Texture texture = new Texture2D();
            TextureAttributes texAttr = new TextureAttributes();
            texAttr.setTextureMode(TextureAttributes.MODULATE);
            texture.setBoundaryModeS(Texture.WRAP);
            texture.setBoundaryModeT(Texture.WRAP);
            texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.0f, 0.0f));
            Material mat = new Material(color, black, color, white, 70f);
            ap.setTextureAttributes(texAttr);
            ap.setMaterial(mat);
            ap.setTexture(texture);
            ColoringAttributes ca = new ColoringAttributes(color,
                            ColoringAttributes.NICEST);
            ap.setColoringAttributes(ca);
            return ap;
    }*/
}
